// Groups 

var XML_GRP={
// group names up to 30
  GROUPNAMES: ["","I","II","III","IV","V",
	       "VI","VII","VIII","IX","X",
	       "XI","XII","XIII","XIV","XV",
	       "XVI","XVII","XVIII","XIX","XX",
	       "XXI","XXII","XXIII","XXIV","XXV",
	       "XXVI","XXVII","XXVIII","XXIX","XXX",
	       "XXXI","XXXII","XXXIII","XXXIV","XXXV"],
  DEFGROUPDOC: "A Priori"
}

function GetGroupsLength() {
  return CLAIMGROUPS.length;
}

function CloneGroups(srcvers) {
  var newgrps=GetClaimSet(srcvers).selectSingleNode("groups");
  var oldgrps=GetClaimSet().selectSingleNode("groups");

  GetClaimSet().replaceChild(newgrps.cloneNode(true),oldgrps);
  CLAIMGROUPS=GetClaimSetGroups();
}



function CreateGroup() {
  var group = XMLDOMDOS.createElement("group");
 
  group.appendChild(CreateGroupArgs(GetGroupsDoc()));
  return group;
}

function AddGroup() {
  CLAIMSET.selectSingleNode("groups").appendChild(CreateGroup());
}

function DeleteGroup(g) {
  CLAIMSET.selectSingleNode("groups").removeChild(CLAIMGROUPS[g-1]);
  var cl_iter= new claim_iterator();
  // rename claim groups to new group idxs 
  while(!cl_iter.end()) {
	var ig=GetGroupIdx(GetClaimGroup(cl_iter.getValue()));
	if(ig==g) {
	  SetClaimGroup(cl_iter.getValue(),"");
        } else if(ig > g) {
	  SetClaimGroup(cl_iter.getValue(),GetGroupName(ig-1));
        }	
        cl_iter.next();
  }	
  //alert("delete");
}

// swap groups g and g+1
function SwapGroupsDown(g) {
   var G1=GetGroupName(g);
   var G2=GetGroupName(g+1);
   if(g < GetGroupsLength()) {
	var node=CLAIMSET.selectSingleNode("groups"); 
        var nodedown;    
	node.insertBefore(node.removeChild(node.childNodes[g]),node.childNodes[g-1]);
   	
  var cl_iter= new claim_iterator();
  // rename claim groups to new group idxs 
  while(!cl_iter.end()) {
	switch(GetClaimGroup(cl_iter.getValue())) {
	  case G1:
	    SetClaimGroup(cl_iter.getValue(),G2);
	    break;
	  case G2:
            SetClaimGroup(cl_iter.getValue(),G1);
	    break;
	  break;
        }	   
        cl_iter.next();
  }	
}
}


function CreateGroupArgsForDoc(doc) {
  for(var g=0 ; g < CLAIMGROUPS.length; g++) {
    if(!CLAIMGROUPS[g].selectSingleNode("groupargs[ @doc = '"+doc+"']")) {
      //alert("Create args for group "+(1+g)+" doc "+doc);
      CLAIMGROUPS[g].appendChild(CreateGroupArgs(doc));
    }
  }
}


function  CreateGroupArgs(doc) {
  var arggroup = XMLDOMDOS.createElement("groupargs");
  arggroup.setAttribute("doc",doc || XML_GRP.DEFGROUPDOC);
  return arggroup;
}


function SetGroupsDoc(doc) {
  CLAIMSET.selectSingleNode("groups").setAttribute("refdoc",doc);
  CreateGroupArgsForDoc(doc);  
}

function GetGroupsDoc() {
  if(CLAIMSET.selectSingleNode("groups")) {
    return CLAIMSET.selectSingleNode("groups").getAttribute("refdoc") || XML_GRP.DEFGROUPDOC;
  } 
  return XML_GRP.DEFGROUPDOC;
}

function RenameGroupDoc(olddoc,newdoc) {
    var groups=CLAIMSET.selectSingleNode("groups");
    if(groups.getAttribute("refdoc")) groups.setAttribute("refdoc",newdoc);
    for(var g=0 ; g < CLAIMGROUPS.length; g++) {
        var node= CLAIMGROUPS[g].selectSingleNode("groupargs[ @doc = '"+olddoc+"']");
        if(node) node.setAttribute("doc",newdoc);
    }
}    
    	

// Access methods 
function SetGroupPaid(g) {   
  if(g) CLAIMGROUPS[g-1].setAttribute("paid","yes");  
}

function SetGroupNotPaid(g) {
  if(g) CLAIMGROUPS[g-1].setAttribute("paid","");  
}

function IsGroupPaid(g) {
  return g==1 || CLAIMGROUPS[g-1].getAttribute("paid");
}

function GetNumGroupsPaid() {
  var nbpaid=0;
  var gr_iter= new group_iterator();
  while(!gr_iter.end()) {
    if(gr_iter.isPaid()) {
      nbpaid++;
    }
    gr_iter.next();
  }
  return nbpaid;
}

function GetAllPaidClaims(){
  var clist= new List();
  var gr_iter= new group_iterator();
  while(!gr_iter.end()) {
    if(gr_iter.isPaid()) {
      clist.push(gr_iter.getClaimList());
    }
    gr_iter.next();
  }
  return clist.unique();
}



function GetGroupAttr(doc,idx,attr) {
  var node=CLAIMGROUPS[idx-1].selectSingleNode("groupargs[ @doc = '"+doc+"']");
  return (node && node.getAttribute(attr)) || "";
}

function SetGroupAttr(doc,idx,attr,val) {
  var node= CLAIMGROUPS[idx-1].selectSingleNode("groupargs[ @doc = '"+doc+"']");
  if(!node) { // backwards compat for formats without group title
    CLAIMGROUPS[idx-1].appendChild(CreateGroupArgs(doc));
    node= CLAIMGROUPS[idx-1].selectSingleNode("groupargs[ @doc = '"+doc+"']");
  }
  if(node) {
    node.setAttribute(attr,val);
  } else {
    alert("Failed setAttribute("+attr+","+val+") for doc "+doc+" g "+idx);
  }
}

function GetGroupPrefClaim(doc,g) {
  return GetGroupAttr(doc,g,"prefcl");
}
function SetGroupPrefClaim(doc,g,cl) {
  SetGroupAttr(doc,g,"prefcl",cl);
}


function GetGroupSTFClaim(doc,g) {
  return GetGroupAttr(doc,g,"stfcl");
}
function SetGroupSTFClaim(doc,g,cl) {
  SetGroupAttr(doc,g,"stfcl",cl);
}

function GetGroupSTF(doc,g) {
  return GetGroupAttr(doc,g,"stf");
}
function SetGroupSTF(doc,g,txt) {
  SetGroupAttr(doc,g,"stf",txt);
}


function GetGroupProblem(doc,g) {
  return GetGroupAttr(doc,g,"problem") || GetGroupAttr(XML_GRP.DEFGROUPDOC,g,"problem");;
}
function SetGroupProblem(doc,g,txt) {
  SetGroupAttr(doc,g,"problem",txt);
}


function GetGroupTitle(doc,g) {
  return GetGroupAttr(doc,g,"title") || GetGroupAttr(XML_GRP.DEFGROUPDOC,g,"title");
}
function SetGroupTitle(doc,g,txt) {
  SetGroupAttr(doc,g,"title",txt);
}




function GetGroupName(idx) {
  return XML_GRP.GROUPNAMES[idx];
} 

function GetGroupIdx(G) {
  for(var g in XML_GRP.GROUPNAMES) {
    if(XML_GRP.GROUPNAMES[g]==G) {
	return g;
    }
  }
  return 0;
}

// Fees to be paid
function GetGroupsFees() {
  var g_iter= new group_iterator();
  var fees=0;
  while(!g_iter.end()) {
    if(!g_iter.isPaid()) {
      fees++;
    }
    g_iter.next();
  }
  return fees;
}

function nu_findprevstfclaims(doc,grname,stcl,outlist) {
  var cl_iter=new List_iterator(list_toList(GetGroupClaims(grname)));
  while(!cl_iter.end()) {
    if(cl_iter.getValue()==stcl) {
      return;
    }
    //alert("No stf in "+cl_iter.getValue());
    outlist.push(cl_iter.getValue());
    cl_iter.next();
  }
}

function nu_nostfclaims(doc) {
  var gr_iter= new group_iterator();
  var nostfclaims=new List();
  while(!gr_iter.end()) {
    var stcl=gr_iter.getSTFCL(doc);
    nu_findprevstfclaims(doc,gr_iter.getName(),stcl,nostfclaims); 
    gr_iter.next();
  }
  //alert("That is "+nostfclaims.getValue());
  return nostfclaims;
}


function group_iterator(init) {
  this._end=GetGroupsLength();
  this._init=init||1;
  this._idx=this._init;
}

group_iterator.prototype.iterator=function() { return new group_iterator(this._init);}
group_iterator.prototype.reduce=function() {  return this;} 
group_iterator.prototype.end = function () { return this._idx > this._end;};
group_iterator.prototype.next= function () { return this._idx++; }
group_iterator.prototype.getValue= function() { return this._idx;}
group_iterator.prototype.getName= function () { return GetGroupName(this._idx);}
group_iterator.prototype.getClaimList = function() { return Group2List(GetGroupName(this._idx));}
group_iterator.prototype.getTitle = function(doc) {  return GetGroupTitle(doc || GetGroupsDoc() ,this._idx);}
group_iterator.prototype.getProblem = function(doc) { return GetGroupProblem(doc || GetGroupsDoc() ,this._idx);}
group_iterator.prototype.getDiff=function(doc) { return find_stfs_claim(doc || GetGroupsDoc() ,this.getSTFCL(doc));}
group_iterator.prototype.getSTF = function(doc) { return  GetGroupSTF(doc || GetGroupsDoc() ,this._idx);}
group_iterator.prototype.isPaid = function() { return IsGroupPaid(this._idx);}
group_iterator.prototype.getSTFCL = function(doc) { return GetGroupPrefClaim(doc || GetGroupsDoc() ,this._idx) 
						    || GetGroupSTFClaim(doc|| GetGroupsDoc() ,this._idx);}
group_iterator.prototype.getSTFPREVCL=function(doc) { return find_prevstclaim(doc|| GetGroupsDoc() ,
									      this.getName(),
									      this.getSTFCL(doc|| GetGroupsDoc() ));}
group_iterator.prototype.length=function() { return this._end;} 
group_iterator.prototype.reset= function() { this._idx=this._init;}

function groupname_iterator(init) {
  this._end=GetGroupsLength();
  this._idx=init||1;
}

groupname_iterator.prototype.end = function () { return this._idx > this._end;};
groupname_iterator.prototype.next= function () { return this._idx++; }
groupname_iterator.prototype.getValue= function () { return GetGroupName(this._idx);}


